<template>
  <div class="register-wrapper">
    <el-row>
      <el-col :span="12" :offset="6">
        <el-card class="register-card">
          <el-form ref="registerForm" :model="registerForm" :rules="registerRules" label-width="80px">
            <h2 class="text-center">注册</h2>
            <el-form-item label="用户名" prop="username">
              <el-input v-model="registerForm.username" placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input v-model="registerForm.password" placeholder="请输入密码" type="password"></el-input>
            </el-form-item>
            <el-form-item label="确认密码" prop="confirmPassword">
              <el-input v-model="registerForm.confirmPassword" placeholder="请再次输入密码" type="password"></el-input>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="register">注册</el-button>
            </el-form-item>
          </el-form>
          <div class="text-center">
            <router-link to="/login">已有账号？去登录</router-link>
          </div>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import axios from "@/request";

export default {
  name: "register",
  data() {
    return {
      registerForm: {
        username: "",
        password: "",
        confirmPassword: "",
      },
      registerRules: {
        username: [
          {required: true, message: "请输入用户名", trigger: "blur"},
        ],
        password: [
          {required: true, message: "请输入密码", trigger: "blur"},
          {min: 6, max: 20, message: "密码长度在6到20个字符之间", trigger: "blur"},
        ],
        confirmPassword: [
          {required: true, message: "请确认密码", trigger: "blur"},
          {validator: this.validatePassword, trigger: "blur"},
        ],
      },
    };
  },
  methods: {
    validatePassword(rule, value, callback) {
      if (value !== this.registerForm.password) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    },
    register() {
      this.$refs.registerForm.validate(valid => {
        if (valid) {
          // 处理注册逻辑
          axios.post("/loginAndRegister/register", {
            username: this.registerForm.username,
            password: this.registerForm.password
          }).then((res) => {
            console.log(res)
            if (res.code === 200) {
              console.log(res.message);
              this.$message.success(res.message);
              this.$router.push('/login')
            } else {
              console.error(res.message);
              this.$message.error(res.message);

            }
          })


          console.log("登录成功");
          console.log("注册成功");
        } else {
          console.log("注册失败");
          return false;
        }
      });
    },
  },
};
</script>

<style scoped>
.register-wrapper {
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

.register-card {
  width: 400px;
  margin-top: 50px;
  padding: 40px;
}

.text-center {
  text-align: center;
  margin-top: 20px;
}
</style>